import unittest
import requests

from api.investment import InvestmentApi
from api.login import LoginAPI
from bs4 import BeautifulSoup


class Test_InvestmentApi(unittest.TestCase):

    @classmethod
    def setUpClass(cls) -> None:
        cls.session = requests.session()
        cls.LoginAPI = LoginAPI()
        resp = cls.LoginAPI.login666(cls.session, {"keywords": "13137812345", "password": "a1008611"})
        print(resp.json())
        cls.Investment = InvestmentApi()
    @classmethod
    def tearDownClass(cls) -> None:
        cls.session.close()

    # 投资
    def test01_Investment(self):
        data_dict = {
            "id":830,
            "depositCertificate":-1,
            "amount":100
        }
        resp = self.Investment.post_investment(session=self.session, data_dict=data_dict)
        print(resp.json())

        # 提取html
        html = resp.json().get('description').get('form')
        # 解析HTML
        suop = BeautifulSoup(html, 'html.parser')
        #  提取url
        html_url = suop.form.get('action')
        # 提取请求参数
        resp_data = {}
        for i in suop.find_all('input'):
            name = i.get('name')
            value = i.get('value')
            resp_data[name] = value
        resp_3 = requests.post(url=html_url, data=resp_data)
        print('第三方投资返回响应数据：',resp_3.text)

    # 我的投资列表
    def test02_mytenderlist(self):
        data_dict = {
            "page":1
        }
        resp = self.Investment.post_mytenderlist(session=self.session, data_dict=data_dict)
        print("我的投资列表返回响应体数据：",resp.json())



if __name__ == '__main__':
    unittest.main()